package com.cao.meet;

import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.blankj.utilcode.util.LogUtils;
import com.google.gson.Gson;
import com.cao.meet.bean.BankBean;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class OkAsyncActivity extends AppCompatActivity {

    private TextView ok_async_text;
    private ProgressBar ok_async_progress;

    private String cms_url = "https://ccdcapi.alipay.com/validateAndCacheCardInfo.json?_input_charset=utf-8&cardNo=6228482128273435971&cardBinCheck=true";
    private String user_name = "";
    private String pass_word = "";
    private TextView card_type;
    private TextView bank_name;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ok_async);

        ok_async_text = (TextView) findViewById(R.id.ok_async_text);
        ok_async_progress = (ProgressBar) findViewById(R.id.ok_async_progress);
        card_type = (TextView) findViewById(R.id.card_type);
        bank_name = (TextView) findViewById(R.id.bank_name);

        new OkAsyncTask().execute(cms_url);

    }

    private String json = null;

    class OkAsyncTask extends AsyncTask<String, Void, String> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            ok_async_progress.setVisibility(View.VISIBLE);
            card_type.setVisibility(View.GONE);
            bank_name.setVisibility(View.GONE);
        }

        @Override
        protected String doInBackground(String... strings) {
            OkHttpClient okHttpClient = new OkHttpClient.Builder().build();
            Request request = new Request.Builder().url(cms_url).build();
            okHttpClient.newCall(request).enqueue(new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {
                    LogUtils.e("请求失败");
                }

                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    LogUtils.e("请求成功");
                    json = response.body().string();
                    LogUtils.e(json);
                }
            });
            try {
                Thread.sleep(400);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return json;
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            LogUtils.e(s);
            ok_async_text.setText(s);
            ok_async_progress.setVisibility(View.GONE);
            Gson gson = new Gson();
            BankBean bankBean = gson.fromJson(s, BankBean.class);
            if (bankBean != null){
                card_type.setVisibility(View.VISIBLE);
                bank_name.setVisibility(View.VISIBLE);
                if (bankBean.getCardType().equalsIgnoreCase("DC")){
                    card_type.setText("储蓄卡");
                }
                if (bankBean.getBank().equalsIgnoreCase("ABC")){
                    bank_name.setText("中国农业银行");
                }
            }
        }
    }

}
